package remoteService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MultiClientHelper {
	String host = "localhost";
	int portNum = 8000;
	
	Socket socket=null;

	public MultiClientHelper() {
		try {
			socket=new Socket(host,portNum);
		} catch (UnknownHostException e) {
		
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		//ClientHelper ch = new ClientHelper();
		MultiClientHelper ch=new MultiClientHelper();
		String reply = ch.sendToNet("Login 1 001 teacherAccounts.txt");
		System.out.println("client:The reply is " + reply);
	}

	public String sendToNet(String send) {
		String reply = "";
	//	Socket socket = null;
		PrintWriter pw = null;
		BufferedReader br = null;
		try {

			pw = new PrintWriter(socket.getOutputStream());
			br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			pw.write(send);
			pw.flush();
			socket.shutdownOutput();
			String temp;
			while ((temp = br.readLine()) != null) {
				reply += temp;
			}
			System.out.println("client get reply" + reply);
		} catch (IOException e) {
			// TODO: handle exception
			System.out.println("errorInSendToNet" + e);
		} finally {
			try {
				pw.close();
				br.close();
				//socket.close();
			} catch (IOException e2) {
				// TODO: handle exception
				System.out.println("Error in close socket" + e2);
			}
		}

		return reply;
	}
}
